package com.as.dao;

import java.util.List;

import com.as.base.BaseDaoImpl;
import com.as.entity.User;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

	@Override
	public User getUserByNameAndPwd(String username, String password) {
		if (username == null || password == null) {
			System.out.println("userid or password == null");
			return null;
		} else {
			String sql = "FROM User u WHERE u.username =? AND u.password =?";
			return (User) getSession().createQuery(sql)
					.setParameter(0, username).setParameter(1, password)
					.uniqueResult();
		}
	}

	@Override
	public List<User> getOneClassUser(int classNum) {
		String sql = "select * from user u where u.classnum =? AND u.level = ?";
		return getSession().createSQLQuery(sql).setParameter(0, classNum)
				.setParameter(1, STUDENTLEVEL).list();
	}

	@Override
	public User getbyname(String username) {
		if (username == null) {
			return null;
		} else {
			String sql = "FROM User u WHERE u.username =:username";
			return (User) getSession().createQuery(sql)
					.setParameter("username", username).uniqueResult();
		}
	}

	@Override
	public Object getbyemail(String email) {
		if (email == null) {
			return null;
		} else {
			String sql = "FROM User u WHERE u.email =:email";
			return (User) getSession().createQuery(sql)
					.setParameter("email", email).uniqueResult();
		}
	}

}
